package jdk.jdk8.lambda;

import java.util.*;

/**
 *
 */
public class OptionTest {
    public static void main(String[] args) {
        // 模拟从数据库中取数据
        Student student = new Student("zk", 19, null);
        List<Student> list = new ArrayList<Student>();
        list.add(student);
        //Class class0201 = new Class("0201", list);

        // 1.
        //Class class0201 = null;
        // 2.
        //Class class0201 = new Class("0201", null);
        // 3.
        List<Student> list1 = new ArrayList<Student>();
        list1.add(student);
        Class class0201 = new Class("0201", list1);

        // 取出分数，如果分数为空这指定默认值0
        //System.out.println(getScore(class0201));
        System.out.println(getScoreByOpt(class0201));

        //other(class0201);
    }

    private static Integer getScoreByOpt(Class class0201) {
        Integer aNull = Optional.ofNullable(class0201)
                .map(Class::getStudents)//  List<Student> students
                .map(students -> students.stream().findFirst().get())// Student
                .map(Student::getScore) // Integer
                .orElseThrow(()->new RuntimeException("null"));
        return aNull;
                //.ifPresent(e->{addValue(e)};);

    }

    private static int getScore(Class class0201) {
        if (Objects.isNull(class0201)) {
            return 0;
        }
        List<Student> students = class0201.getStudents();
        if (Objects.isNull(students) || students.size() > 1) {
            return 0;
        }
        Student student1 = students.get(0);
        if (Objects.isNull(student1)) {
            return 0;
        }
        return Objects.nonNull(student1.getScore()) ? student1.getScore() : 0;
    }

    private static void other(Class class0201) {
        // 获取学生姓名
        String studentName = "";
        //studentName = getString(class0201, studentName);
        studentName = getStudentNameByLambda(class0201);
        System.out.println(studentName);

//        Optional.ofNullable(class0201)
//                .map(Class::getStudents)
//                .map(students -> students.stream().findAny().get())
//                .map(Student::getName)
//                .ifPresent(n-> System.out.println(n));

        /*
            flatMap 与 map 的区别
        *   1.map中返回值是 Optional时会多包一层Optional: Optional<Optional<Integer>> integer
            2.flatMap 的返回值必须是Optional,但是外层不会多包一层Optional: Optional<Integer> integer
        * */
        // Optional<Integer> integer 
        Optional<Optional<Integer>> integer = Optional.ofNullable(class0201)
                .map(Class::getStudents)
                .map(stus -> stus.stream().findAny().get())
                .map(Student::getAge)
                .map(e -> addValue(e));
        //.flatMap(e -> addValue(e));


        //integer.ifPresent(n-> System.out.println(n));
    }

    // 返回值是Optional
    public static Optional<Integer> addValue(Integer age) {
        return Optional.ofNullable(age + 2);
    }

    // 返回值是Optional
    public static Integer addValue2(Integer age) {
        return age + 2;
    }


    private static String getStudentNameByLambda(Class class0201) {
        return Optional.ofNullable(class0201)
                .map(Class::getStudents)
                .map(students -> students.stream().findAny().get())
                .map(Student::getName).orElse("");
    }

    private static String getString(Class class0201, String studentName) {
        List<Student> students = class0201.getStudents();
        if (students != null && students.size() > 0) {
            Student s = students.get(0);
            if (s != null) {
                if (s.getName() != null) {
                    studentName = s.getName();
                }
            }
        }
        return studentName;
    }
}
